@extends('admin.admin')

@section('content')
<link rel="stylesheet" href="{{ asset('/js/bootstrap-switch/css/bootstrap-switch.min.css') }}">

<style>
    .checkbox-inline{ padding-left: 0;}
    .form-horizontal .has-feedback .form-control-feedback { right: 0;}
    .select2-container { width: 100% !important;}
</style>
<!-- Content Header (Page header) -->
<section class="content-header">
    <h1>
        产品管理
        <small>产品修改</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="/admin"><i class="fa fa-dashboard"></i> 控制面板</a></li>
        <li ><a href="/admin/goods">产品管理</a></li>
        <li class="active">产品列表</li>
        <li class="active">产品修改</li>
    </ol>
</section>

<?php

Form::macro(
        'selectRequired',
        function($name, $options = [], $selected = null, $attributes = [], $disabled = [])
        {
            $html = '<select name="' . $name . '"';
            foreach ($attributes as $attribute => $value)
            {
                $html .= ' ' . $attribute . '="' . $value . '"';
            }
            $html .= '>';

            foreach ($options as $value => $text)
            {
                $html .= '<option value="' . $value . '"' .
                        ($value == $selected ? ' selected="selected"' : '') .
                        (in_array($value, $disabled) ? ' disabled="disabled"' : '') . '>' .
                        $text . '</option>';
            }

            $html .= '</select>';

            return $html;
        }
);?>


<!-- Main content -->
<section class="content">
<div class="row">
        <div class="col-md-12">
            <div class="box box-info">
                <div class="box-header with-border">
                    <h3 class="box-title">修改产品</h3>
                </div>
                <!-- /.box-header -->

                <!-- form start -->
                {!! Form::model(@$product_info,array('route' => 'admin.product.update','method' => 'PUT','class'=>'J-chkForm form-horizontal')) !!}
                    <div class="box-body">

                        <div class="form-group">
                            <label class="col-sm-2 control-label">产品名称</label>
                            <div class="col-sm-6">
                                {!! Form::text('product_name',@$product_info['product_name'],['class'=>'form-control', 'id'=>'J-nameInput','placeholder'=>'请填写产品名称','required'])!!}
                            </div>
                        </div>

                        <?php $select = $category::getTreeList();?>

                        <?php

                        function myspace($n,$hasChild){
                            $enum = ['├','└'];
                            if ($hasChild){
                                $space = $enum[0];
                            }else{
                                $space = $enum[1];
                            }
                            $str = '';
                            for ($i=0;$i<=$n;$i++){
                                $str.='&nbsp;&nbsp;';
                            }
                            return $str.$space;
                        }
                        $seletcItem[0] = '所有商品分类';

                        $disabledOption = [];
                        foreach ($select as $k=> $item){

                            if($item->has_children){
                                $disabledOption[]=$item->cat_id;
                            }
                            $seletcItem [$item->cat_id]= myspace($item->deepth,$item->has_children).$item->cat_name;
                        }

                        ?>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">产品类型</label>
                            <div class="J-type col-sm-6">
                                {!! Form::selectRequired('cat_id',$seletcItem,@$product_info['cat_id'],['class'=>'form-control select2','id'=>'J-proType','data-placeholder'=>'请选择商品分类' ],$disabledOption)!!}
                            </div>
                        </div>

                        <div class="J-checks">

                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">全部属性组合</label>
                            <div class="col-sm-10">
                                <!-- /.box-header -->
                                <div id="productBox">
                                    <div class="form-control-static">无</div>
                                </div>
                                <!-- /.box-body -->

                                <template class="J-tableTpl">
                                    <table class="table table-bordered table-striped-">
                                        <thead></thead>
                                        <tbody></tbody>
                                        <tfoot></tfoot>
                                    </table>
                                </template>

                            </div>
                        </div>
                    </div>
                    <!-- /.box-body -->
                    <div class="box-footer">
                        <input type="hidden" id="productId" value="{{$product_info['product_id']}}">

                        <div class="col-sm-offset-2">
                            <button type="submit" class="J-submit btn btn-info" disabled style="margin-left:4px;width: 180px;">确定</button>
                        </div>
                    </div>
                    <!-- /.box-footer -->
                {!! Form::hidden('_method', 'PUT') !!}
                {!! Form::close() !!}

            </div>
            <!-- /.box -->

        </div>

    </div>



</section>
<!-- /.content -->

<template id="groupTpl">
    <div class="form-group">
        <label class="col-sm-2 control-label">${attribute_name}</label>
        <div class="J-params col-sm-10" data-name="${attribute_name}" data-id="${id}">
            ${checkHtml}
        </div>
    </div>
</template>
<template id="checkTpl">
    <div class="checkbox-inline">
        <label>
            <input id="checkBox_${id}" type="checkbox" data-id="${id}" class="minimal">
            ${attribute_value}
        </label>
    </div>
</template>

<div id="alertModal" class="modal fade modal-danger" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                <h4 class="modal-title" id="mySmallModalLabel">提示信息</h4>
            </div>
            <div class="modal-body">
            </div>
        </div><!-- /.modal-content -->
    </div>
</div>

<!-- Select2 -->
<script type="text/javascript" src="{{ asset("/components/AdminLTE/plugins/select2/select2.full.min.js") }}"></script>

<!-- iCheck 1.0.1 -->
<script src="{{ asset("/components/AdminLTE/plugins/iCheck/icheck.min.js")}}"></script>

<!-- 页面JS -->
<script src="{{ asset("/js/admin.product.js")}}"></script>
<script src="{{ asset("/js/tools.js")}}"></script>

<script>
    //Initialize Select2 Elements
    $(".select2").select2();

    //初始化产品列表
    var products = new Products({type: 'edit'});

    var data='<?php echo(json_encode($product_info)); ?>';
    products.init(data);

</script>
@endsection